package com.jkgh.remedium.rsgen.generator.doc;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

import com.jkgh.remedium.rsgen.vo.*;

public class RemediumDataObjectDocGenerator {

	public static void generate(File outputDir, DataObject d, RemediumSystem rs) throws FileNotFoundException {

		File outFile = new File(outputDir, d.getClassName() + ".html");
		PrintStream out = new PrintStream(outFile);

		out.println("<html>");
		out.println("<head>");
		out.println("<title>" + d.getClassName() + " Remedium Data Object Documentation</title>");
		out.println("</head>");
		out.println("<body bgcolor=\"#ddddff\">");
		out.println("<h1>" + d.getClassName() + "</h1>");
		out.println("<h2>" + d.getDescription() + "</h2>");
		out.println("<br>");

		out.println("<h2>Fields:</h2>");
		out.println("<ul>");
		for (FieldInfo f : d.getFields()) {
			out.println("<li>");
			out.println("<h3>" + RemediumSystemDocGenerator.linkToClass(f.getClassName()) + " " + f.getName() + "</h3>");
			out.println(f.getDescription());
			out.println("</li>");
			out.println("<br>");
		}
		out.println("</ul>");

		out.println("<br>");
		out.println("<h2>Usages:</h2>");
		out.println("<ul>");
		for (DataObject sd : rs.getDataObjects()) {
			for (FieldInfo sdf : sd.getFields()) {
				if (sdf.getClassName().equals(d.getClassName())) {
					out.println("<li>");
					out.println("As field <b>" + sdf.getName() + "</b> in <b>" + RemediumSystemDocGenerator.linkToClass(sd.getClassName()) + "</b>");
					out.println("</li>");
					out.println("<br>");
				} else if (RemediumSystemDocGenerator.unArray(sdf.getClassName()).equals(d.getClassName())) {
					out.println("<li>");
					out.println("As array field <b>" + sdf.getName() + "</b> in <b>" + RemediumSystemDocGenerator.linkToClass(sd.getClassName()) + "</b>");
					out.println("</li>");
					out.println("<br>");
				}
			}
		}
		for (MethodObject sm : rs.getMethodObjects()) {
			for (MethodInfo smm : sm.getMethods()) {
				for (Argument smma : smm.getArguments()) {
					if (smma.getClassName().equals(d.getClassName())) {
						out.println("<li>");
						out.println("As argument <b>" + smma.getName() + "</b> of method <b>" + smm.getName() + "()</b> in <b>" + RemediumSystemDocGenerator.linkToClass(sm.getClassName()) + "</b>");
						out.println("</li>");
						out.println("<br>");
					} else if (RemediumSystemDocGenerator.unArray(smma.getClassName()).equals(d.getClassName())) {
						out.println("<li>");
						out.println("As array argument <b>" + smma.getName() + "</b> of method <b>" + smm.getName() + "()</b> in <b>" + RemediumSystemDocGenerator.linkToClass(sm.getClassName())
							+ "</b>");
						out.println("</li>");
						out.println("<br>");
					}
				}
				if (smm.getReturnType() != null) {
					if (smm.getReturnType().getClassName().equals(d.getClassName())) {
						out.println("<li>");
						out.println("As result of method <b>" + smm.getName() + "()</b> in <b>" + RemediumSystemDocGenerator.linkToClass(sm.getClassName()) + "</b>");
						out.println("</li>");
						out.println("<br>");
					} else if (RemediumSystemDocGenerator.unArray(smm.getReturnType().getClassName()).equals(d.getClassName())) {
						out.println("<li>");
						out.println("As array result of method <b>" + smm.getName() + "()</b> in <b>" + RemediumSystemDocGenerator.linkToClass(sm.getClassName()) + "</b>");
						out.println("</li>");
						out.println("<br>");
					}
				}
			}
		}
		out.println("</ul>");

		out.println("</body>");
		out.println("</html>");

		out.close();
	}

}
